﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.Routing;

namespace MyLab.Mvc
{
    public class MainsiteMenu : MyLab.Interfaces.IMenu
    {
        MenuItemCollection _menus = new MenuItemCollection(
            new MainsiteMenuItem("home", "Index", "Home", true),
            new MainsiteMenuItem("blog", "Blog", "Home"),
            new MainsiteMenuItem("about", "About", "Home"),
            new MainsiteMenuItem("projects", "List", "Project")
            );
        string _curr_controllerName;
        string _curr_actionName;

        public MainsiteMenu(RouteValueDictionary routData)
            : this(routData, null)
        { }

        public MainsiteMenu(RouteValueDictionary routData, MenuItemCollection menus)
        {
            if (routData != null)
            {
                _curr_actionName = routData["action"].ToString();
                _curr_controllerName = routData["controller"].ToString();
            }
            if (menus != null)
            {
                _menus.Clear();
                _menus = menus;
            }
        }

        #region IMenu 成员

        public string CreateMenu(string currentItemCssClass)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var menu in _menus)
            {
                if ((menu.ActionName == _curr_actionName) && (menu.ControllerName == _curr_controllerName))
                    menu.IsCurrent = true;
                else
                    menu.IsCurrent = false;
                sb.Append(menu.RenderItem(currentItemCssClass));
            }

            return sb.ToString();
        }

        #endregion
    }
}
